﻿function Save(CaseType,CaseIndex)
{
	DBNo=document.getElementById("divDBNo").value;
	Payer=document.getElementById("divPayer").value;
	Subject=document.getElementById("divSubject").value;
	Attention=document.getElementById("divAttention").value;
	CurrencyIndex=document.getElementById("divCurrency").selectedIndex;
	Currency=document.getElementById("divCurrency").options[CurrencyIndex].value;
	Signer=document.getElementById("divSigner").value;
	Title=document.getElementById("divTitle").value;
	thueVat=document.getElementById("divPercentVat").value;
	DebitNotePrintAccountIndex=document.getElementById("divSelectAccount").selectedIndex;
	DebitNotePrintAccountId=document.getElementById("divSelectAccount").options[DebitNotePrintAccountIndex].value;
	dDate=document.getElementById("divCreateDate").value;
	
	ShortName=document.getElementById("txtShortCustomerName").value;
	CustomerName=document.getElementById("txtCustomerName").value;
	Address=document.getElementById("txtCustomerAddress").value;
	Phone=document.getElementById("txtCustomerPhone").value;
	insideDepartmentIndex=document.getElementById("drpSelectDepartment").selectedIndex;
	insideDepartmentId=document.getElementById("drpSelectDepartment").options[insideDepartmentIndex].value;
		
	pType=document.getElementById("drpSelectPType").options[document.getElementById("drpSelectPType").selectedIndex].value;
		
	CustomerFieldId=document.getElementById("drpSelectCustomerField").options[document.getElementById("drpSelectCustomerField").selectedIndex].value;
	CustomerServiceId=document.getElementById("drpSelectCustomerService").options[document.getElementById("drpSelectCustomerService").selectedIndex].value;
		
	fromLocationId=document.getElementById("selectFromDepartment").options[document.getElementById("selectFromDepartment").selectedIndex].value;
	toLocationId=document.getElementById("selectToDepartment").options[document.getElementById("selectToDepartment").selectedIndex].value;

	if(document.getElementById("divCaseType")!=null)
	{
		CaseType=document.getElementById("divCaseType").innerHTML;
		CaseIndex=document.getElementById("divCaseIndex").value;
	}
		
		
	EnglishAccount=document.getElementById("divAccountEnglish").value;
	VietnameseAccount=document.getElementById("divAccountVietnamese").value;
			
	Max=12;
	SaveItemData="";
	for(var iIndex=0;iIndex<Max;iIndex++)
	{
		Item=document.getElementById("txtIndex_"+iIndex).value;
		Description=document.getElementById("txtDescription_"+iIndex).value;
		Fee=document.getElementById("txtProFee_"+iIndex).value;
		NOIP=document.getElementById("txtOffFee_"+iIndex).value;
		Postage=document.getElementById("txtOther_"+iIndex).value;
		PercentDiscount=document.getElementById("txtDiscount_"+iIndex).value;
			
			
		SaveItemData=SaveItemData+Description;
		SaveItemData=SaveItemData+"~"+Fee;
		SaveItemData=SaveItemData+"~"+NOIP;
		SaveItemData=SaveItemData+"~"+Postage;
		SaveItemData=SaveItemData+"~"+PercentDiscount;
		SaveItemData=SaveItemData+"~"+Item;
		if(iIndex<Max-1)
		{
			SaveItemData=SaveItemData+"|";
		}
	}

DBNIndex = Portal.Vision.AddDebitNote.ServerSideAddDebitNote(CaseType, CaseIndex, dDate, DebitNotePrintAccountId, DBNo, Payer, Subject, Attention, Currency, Signer, Title, EnglishAccount, VietnameseAccount, thueVat, ShortName, CustomerName, Address, Phone, pType, fromLocationId, toLocationId, SaveItemData, insideDepartmentId, CustomerFieldId, CustomerServiceId).value;
	if(DBNIndex!=null)
	{
		if(DBNIndex.indexOf("ERROR")!=-1)
		{
			alert(DBNIndex);
			return;
		}
	}
		
	Url=Portal.Vision.AddDebitNote.ServerSideGetViewUrl(DBNIndex).value;
	alert("Phát hành DebitNote thành công!");
	window.open(Url,"_self");
}
	
function ClientDetectDebitNoFunc()
{
	CaseIndex=document.getElementById("divCaseIndex").value;
	CaseType=document.getElementById("divCaseType").innerHTML;
	RetValue=Portal.Vision.AddDebitNote.GetDebitNodeNo(CaseType,CaseIndex).value;
	if(RetValue!=null)
	{
		if(RetValue.indexOf("ERROR")!=-1)
		{
			alert(RetValue);
			return;
		}
	}
	document.getElementById("divDBNo").value=RetValue;
}
	
	
function DetectAccountFunc()
{
	selectedIndex=document.getElementById("divSelectAccount").selectedIndex;
	Id=document.getElementById("divSelectAccount").options[selectedIndex].value;
	Vietnamese=Portal.Vision.AddDebitNote.GetVietnamese(Id).value;
	English=Portal.Vision.AddDebitNote.GetEnglish(Id).value;
	document.getElementById("divAccountEnglish").value=English;
	document.getElementById("divAccountVietnamese").value=Vietnamese;
}
	
function DetectSignerTitle()
{
	Signer=document.getElementById("divSigner").value;
	RetValue=Portal.Vision.AddDebitNote.ServerSideDetectSignerTitle(Signer).value;
	if(RetValue.indexOf("ERROR")!=-1)
	{
		alert(RetValue);
		return;
	}
	document.getElementById("divTitle").value=RetValue;
}
	
	
function DetectOriginCustomer()
{
	CustIndex=document.getElementById("divCustIndex").value;
	ShortName=Portal.Vision.AddDebitNote.ServerGetCustomerInfo(CustIndex,"ShortName").value;
	if(ShortName.indexOf("ERROR")!=-1)
	{
		alert(ShortName);
		return;
	}
		
	document.getElementById("txtShortCustomerName").value=ShortName;
		
	CustName=Portal.Vision.AddDebitNote.ServerGetCustomerInfo(CustIndex,"CustName").value;
	if(CustName.indexOf("ERROR")!=-1)
	{
		alert(CustName);
		return;
	}
		
	document.getElementById("txtCustomerName").value=CustName;
		
	Address=Portal.Vision.AddDebitNote.ServerGetCustomerInfo(CustIndex,"Address").value;
	if(Address.indexOf("ERROR")!=-1)
	{
		alert(Address);
		return;
	}
	document.getElementById("txtCustomerAddress").value=Address;
		
		
	Phone=Portal.Vision.AddDebitNote.ServerGetCustomerInfo(CustIndex,"Phone").value;
	if(Phone.indexOf("ERROR")!=-1)
	{
		alert(Address);
		return;
	}
	document.getElementById("txtCustomerPhone").value=Phone;
}
